﻿using System;
using Bricks.Data;

namespace Bricks.EntityFramework.Data
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class DbContextExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="contextFacade"></param>
        /// <param name="action"></param>
        public static void Execute(this IContextFacade contextFacade, Action<IDbContextContextFacade> action)
        {
            contextFacade.Execute<bool>(dbContextContextFacade =>
            {
                action.Invoke(dbContextContextFacade);

                return true;
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="contextFacade"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static TResult Execute<TResult>(this IContextFacade contextFacade, Func<IDbContextContextFacade, TResult> action)
        {
            return action.Invoke((IDbContextContextFacade)contextFacade);
        }
    }
}
